<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2024/12/28
 * 描述:
 */

namespace app\result;

use app\model\Notice;

class NoticeResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new Notice();
    }

    public function list_for_api($args = [])
    {
        $where = [];
        $where[] = ['status', '=', 1];
        $where[] = ['start_time', '<=', date('Y-m-d H:i:s')];
        $where[] = ['end_time', '>=', date('Y-m-d H:i:s')];
        $noticeList = $this->model->data_list_nopage($where, 'id,title,content,created_at', 'id desc');
        if ($noticeList) {
            foreach ($noticeList as &$item) {
                $item['content'] = htmlspecialchars_decode($item['content']);
            }
        }

        return self::success_return('success', [
            'list' => $noticeList,
            'title_list' => $noticeList ? array_column($noticeList, 'title') : []
        ]);
    }

    public function list($args = [])
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $where = [];
        if (!empty($args['keyword'])) {
            $where[] = ['title', 'like', '%' . trim($args['keyword']) . '%'];
        }
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', 'id desc', $page, $pageSize);
        if ($data) {
            foreach ($data as &$item) {
                $item['content'] = mb_substr(strip_tags(htmlspecialchars_decode($item['content'], true)), 0, 200) . '...';
                $item['status_text'] = $item['status'] == -1 ? '已禁用' : '已启用';
            }
            unset($item);
        }
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data
        ]);
    }

    public function get($args = [])
    {
        $id = intval($args['id'] ?? 0);
        $data = $this->model->data_get(['id' => $id]);
        $data['content'] = htmlspecialchars_decode($data['content']);
        return $this->success_return('success', $data);
    }

    public function save_data($args): array
    {
        $id = intval($args['id'] ?? 0);
        $baseData = [
            'title' => $args['title'],
            'content' => $args['content'],
            'status' => $args['status'] ?? 1,
            'start_time' => $args['start_time'] ?? date('Y-m-d H:i:s'),
            'end_time' => $args['end_time'] ?? date('Y-m-d H:i:s', strtotime('+1 day')),
        ];
        if ($id) {
            $this->model->data_update(['id' => $id], $baseData);
        } else {
            $this->model->data_add($baseData);
        }
        return $this->success_return('保存成功');
    }

    public function del_data($args): array
    {
        $id = intval($args['id'] ?? 0);
        $res = $this->model->data_del(['id' => $id]);
        return $this->success_return('success');
    }
}
